package com.abc.controller;

import com.abc.common.Result;
import com.abc.dto.request.ChatRequestDto;
import com.abc.dto.request.CreateSessionRequestDto;
import com.abc.dto.request.GenerateTitleRequestDto;
import com.abc.dto.request.KnowledgeRequestDto;
import com.abc.dto.response.ChatResponseDto;
import com.abc.dto.response.CreateSessionResponseDto;
import com.abc.dto.response.GenerateTitleResponseDto;
import com.abc.dto.response.KnowledgeResponseDto;
import com.abc.service.ChatService;
import com.abc.service.SessionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/chat")
@Tag(name = "对话接口")
@Validated
public class ChatController {

    @Autowired
    private ChatService chatService;

    @Autowired
    private SessionService sessionService;


    @PostMapping(value = "/send", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "对话接口")
    public Flux<ChatResponseDto> send(@RequestBody ChatRequestDto chatRequestDto) {
        return chatService.send(chatRequestDto);
    }

    @PostMapping(value = "/create/session")
    @Operation(summary = "创建会话")
    public Result<CreateSessionResponseDto> createSession(@RequestBody CreateSessionRequestDto requestDto) {
        return Result.success(sessionService.createSession(requestDto));
    }

    @PostMapping(value = "/generate/title")
    @Operation(summary = "生成标题")
    public Result<GenerateTitleResponseDto> generateTitle(@RequestBody @Valid GenerateTitleRequestDto requestDto) {
        return Result.success(sessionService.generateTitle(requestDto));
    }

    @PostMapping(value = "/knowledge", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "RAG对话接口")
    public Flux<KnowledgeResponseDto> knowledge(@RequestBody @Valid KnowledgeRequestDto chatRequestDto) {
        return chatService.knowledge(chatRequestDto);
    }
}
